import { _decorator, Component, Enum, Node, tween, Vec3, Quat, TweenEasing } from 'cc';
const { ccclass, property } = _decorator;

// 定义动画类型枚举
export enum ITweenType {
    NONE = 0,
    MOVE = 1,
    ROTATE = 2,
    SCALE = 3,
}

// 定义循环类型枚举
export enum LoopType {
    NONE = 0,
    REPEAT = 1,
    PINGPONG = 2
}

// 创建枚举包装对象
const ITweenTypeEnum = Enum(ITweenType);
const LoopTypeEnum = Enum(LoopType);

@ccclass('ITween')
export class ITween extends Component {
    // 基础设置
    @property({
        type: ITweenTypeEnum,
        tooltip: "选择动画类型"
    })
    public animType: ITweenType = ITweenType.NONE;

    @property({
        tooltip: "是否自动播放"
    })
    public autoPlay: boolean = true;

    @property({
        tooltip: "动画持续时间(秒)"
    })
    public duration: number = 1.0;

    // @property({
    //     type: TweenEasing,

    // })
    // public easingType: TweenEasing = TweenEasing.QuadInOut;

    @property({
        type: LoopTypeEnum,
        tooltip: "循环类型"
    })
    public loopType: LoopType = LoopType.NONE;

    @property({
        tooltip: "循环次数(0表示无限循环)",
        visible: function (this: ITween) { return this.loopType !== LoopType.NONE; }
    })
    public loopCount: number = 0;

    // 动画参数
    @property({
        tooltip: "移动目标位置",
        visible: function (this: ITween) { return this.animType === ITweenType.MOVE; }
    })
    public moveTargetPos: Vec3 = new Vec3(0, 0, 0);

    @property({
        tooltip: "旋转角度(度)",
        visible: function (this: ITween) { return this.animType === ITweenType.ROTATE; }
    })
    public rotateAngle: number = 360;

    @property({
        tooltip: "缩放比例",
        visible: function (this: ITween) { return this.animType === ITweenType.SCALE; }
    })
    public scaleRatio: Vec3 = new Vec3(2, 2, 2);

    // 回调设置
    // @property({
    //     type: Node,
    //     tooltip: "回调目标节点"
    // })
    // public callbackTarget: Node | null = null;

    // @property({
    //     tooltip: "动画完成回调方法名"
    // })
    // public callbackMethod: string = "";

    private currentTween: any = null;

    onLoad() {
        if (this.autoPlay) {
            this.playAnimation();
        }
    }

    /**
     * 播放动画
     */
    public playAnimation(): void {
        this.stopAnimation();

        let tweenInstance: any;

        switch (this.animType) {
            case ITweenType.MOVE:
                tweenInstance = tween(this.node)
                    .to(this.duration, { position: this.moveTargetPos }, { easing: "linear" });
                break;
            case ITweenType.ROTATE:
                const quat = new Quat();
                Quat.fromEuler(quat, 0, this.rotateAngle, 0);
                tweenInstance = tween(this.node)
                    .to(this.duration, { rotation: quat }, { easing: "linear" });
                break;
            case ITweenType.SCALE:
                tweenInstance = tween(this.node)
                    .to(this.duration, { scale: this.scaleRatio }, { easing: "linear" });
                break;
            default:
                console.log("未选择动画类型");
                return;
        }

        // 设置循环
        if (this.loopType !== LoopType.NONE) {
            if (this.loopType === LoopType.PINGPONG) {
                tweenInstance = tweenInstance.reverse();
            }
            if (this.loopCount > 0) {
                tweenInstance = tweenInstance.repeat(this.loopCount * (this.loopType === LoopType.PINGPONG ? 2 : 1));
            } else {
                tweenInstance = tweenInstance.repeatForever();
            }
        }

        // 设置回调
        // if (this.callbackTarget && this.callbackMethod) {
        //     tweenInstance = tweenInstance.call(() => {
        //         if (this.callbackTarget && this.callbackMethod) {
        //             this.callbackTarget.emit(this.callbackMethod);
        //         }
        //     });
        // }

        this.currentTween = tweenInstance.start();
    }

    /**
     * 停止动画
     */
    public stopAnimation(): void {
        if (this.currentTween) {
            tween(this.node).stop();
            this.currentTween = null;
        }
    }

    /**
     * 重置到初始状态
     */
    public resetAnimation(): void {
        this.stopAnimation();
        this.node.position = Vec3.ZERO;
        this.node.rotation = Quat.IDENTITY;
        this.node.scale = Vec3.ONE;
    }
}